package deepbrain.simnetwork.monitor;

import java.io.PrintStream;
import java.io.PrintWriter;

import deepbrain.simnetwork.task.SimulationTask;

/**
 * 
 * 
 * @author Li Yang
 * @create 2009-2-25
 */
public class DisplayNetworkMonitor extends MonitorAdapter<SimulationTask>
		implements SimTaskMonitor {

	private PrintWriter out;
	private int numOfSteps;
	private boolean brief = false;

	public DisplayNetworkMonitor() {
		this(System.out);
	}

	public DisplayNetworkMonitor(PrintWriter out) {
		this.out = out;
	}

	public DisplayNetworkMonitor(PrintStream out) {
		this.out = new PrintWriter(out);
	}

	public DisplayNetworkMonitor(PrintWriter out, boolean brief) {
		this.out = out;
		this.brief = brief;
	}

	public DisplayNetworkMonitor(PrintStream out, boolean brief) {
		this.out = new PrintWriter(out);
		this.brief = brief;
	}

	@Override
	public void onTaskStart(SimulationTask task) {
		if (out != null) {
			if (brief) {
				out.println("0.\t" + task.getNetworkState());
			} else {
				out.println("Initial Network State: ");
				if (brief) {
					out.println(task.getNetworkState());
				} else {
					out.println(task.getNetworkState().toStringBrief()+"\n");
				}
				out.println("Task " + task + " Started!\n");
				out.flush();
			}
			out.flush();
		}
	}

	@Override
	public void onStepFinish(SimulationTask task) {
		numOfSteps++;
		if (out != null) {
			if (brief) {
				out.println(numOfSteps + ".\t" + task.getNetworkState());
			} else {
				out.println(task.getTaskName() + " After " + numOfSteps
						+ " Steps: ");
				out.println(task.getNetworkState().toStringBrief() + "\n");
			}
			out.flush();
		}
	}

	@Override
	public void onTaskFinish(SimulationTask task) {
		if (out != null) {
			if (brief) {
				out.println("FINISHED----------------------------------------");
			} else {
				out.println("Task " + task + " Finished!");
			}
			out.flush();
		}
	}

}
